<?php


class CFenlei extends Zend_Db_Table{

	public $flid;

	public $fenleiString;

	public $fenleiArray;
	
	public $fenleiLang='flname';
	
	public $_name;
	
	public $_primary;
	
	private $selectid='flSelect';
	
	public $order='flid ASC';
	
	public function __construct($type){
		
		$this->_primary="flid";
		
		$this->_name=R_PRE . $type . "fls";
		
		parent::__construct();
		
	}
	
	public function setOrder($order){
		
		$this->order=$order;
		
	}


	public function createFenlei($data) {

		$uid=$this->insert($data);
		
		return $uid;

	}
	
	public function setSelectid($selectid){
		
		$this->selectid=$selectid;
		
	}
	
	public function getAllFenlei(){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'*');
                if($this->_name==R_PRE . "product" . "fls")
                {
                    $select->where("openflag=1");
                }		
		//$select->order("pid ASC");
		
		$select->order("flid ASC");
		
		return $db->fetchAll($select);
		
	}
	
	public function getFenleiById($id){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'*');
		$where=$db->quoteInto('flid = ?', $id);
        $select->where($where);
		
		return $db->fetchAll($select);
		
	}
	
	public function getAllFenleiByOrder(){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'*');
		
		//$select->order("pid ASC");
		
		$select->order("en_flname ASC");
		
		return $db->fetchAll($select);
		
	}
	
	public function getAll($data,$flid,$class='fenleiEffection'){
		
		static $a=0;
		
		if($a==0){
			
			$this->fenleiString.='<div>';
			
		}else{
			
			$this->fenleiString.='<div class="smallChildDiv">';
			
		}
		
		$a=1;
		
		for($i=0;$i<count($data);$i++){
			
			if($data[$i]['pid']==$flid){
				
				$this->fenleiString.='<div class="fenleiParentDiv">';

				$this->fenleiString.='<div class="bigChildDiv"><a name="'.$data[$i]['flid'].'" onclick="'.$class.'.showOrHide(this,'.$data[$i]['flid'].');$(\'#flid\').val(' .$data[$i]['flid']. ')">'.$data[$i][$this->fenleiLang].'</a></div>'."\n";
				
				$thisFlid=$data[$i]['flid'];

				$data[$i]=null;
				
				if(count($data)>0){
					
					$this->getAll($data,$thisFlid,$class);
						
				}
				$this->fenleiString.='</div>';
				
			}
			
		}
		
		$this->fenleiString.='</div>';
		
	}
	
	public function getAllNew($data,$flid,$class='fenleiEffection')
	{		
		$this->fenleiString = '';
		for($i=0;$i<count($data);$i++){
			
			if($data[$i]['pid']==$flid){

				$this->fenleiString.='<a class="spbg2" href="javascript:void(0)" name="'.$data[$i]['flid'].'" onclick="'.$class.'.showOrHide(this,'.$data[$i]['flid'].');">'.$data[$i][$this->fenleiLang].'</a>'."\n";
				
				$thisFlid=$data[$i]['flid'];

				$data[$i]=null;
				
				if(count($data)>0){
					
					$this->getAll($data,$thisFlid,$class);
						
				}
				
			}
			
		}		
	}

        public function getoneFenlei($itemid,$where=null){

		$db=$this->getAdapter();

		$select=$db->select();

		$select->from($this->_name,'*');

		$select->where("flid=?",$itemid);

		if($where!=null){

			$select->where($where);

		}

		return $db->fetchAll($select);

	}

	public function getAllSelect($data,$flid,$javascript=''){
		
		$allString='';
		
		$newData=array();
		
		$flCounts=count($data);
		
		for($i=0;$i<$flCounts;$i++){
			
			$newData[$data[$i]['flid']]['pid']=$data[$i]['pid'];
			
			$newData[$data[$i]['flid']][$this->fenleiLang]=$data[$i][$this->fenleiLang];
			
		}
		
		$pid=0;
		
		if(isset($newData[$flid]['pid'])){
		
			$pid=$newData[$flid]['pid'];
		
		}

		do{
			
			$string='';
			
			foreach($newData as $key => $value){
				
				if($value['pid']==$pid){
					
					if($key==$flid){

						$string.='<option selected value="'.$key.'">'.$value[$this->fenleiLang].'</option>';
						
					}else{
						
						$string.='<option value="'.$key.'">'.$value[$this->fenleiLang].'</option>';
						
					}
					
				}
				
			}
			
			if($string!=''){
				
				$string='<select id="'.$this->selectid.'" onchange="'.$javascript.'"><option value="0">Choose</option>'.$string.'</select>';
			
			}
			
			$allString=$string.' '.$allString;
			
			if($pid==0){
				
				break;
				
			}
			
			$flid=$pid;
			
			$pid=$newData[$pid]['pid'];
		
		}while($pid>=0);

		return $allString;
		
	}
	

	public function delFenlei($flid) {
		
		$db=$this->getAdapter();
		
		$fenleiIdArray=$db->fetchAll( "SELECT flid FROM ".$this->_name." WHERE pid='$flid'" );
		
		$where = $db->quoteInto('flid = ?', $flid);
		
		$this->delete($where);

		if (count ( $fenleiIdArray ) > 0) {
			
			for($i=0;$i<count($fenleiIdArray);$i++){
				
				$where = $db->quoteInto('flid = ?', $fenleiIdArray[$i]['flid']);
				
				$this->delete($where);

				$this->delFenlei ( $fenleiIdArray[$i]['flid'] );		
				
			}
			
			return true;


		} else {

			return false;

		}

	}


	public function editFenlei($flid, $thisArray) {
	
		$db=$this->getAdapter();
		
		$where=$db->quoteInto('flid = ?', $flid);
		
		$db->update($this->_name, $thisArray, $where);

	}


	
	public function getAllFlCounts(){
		
		$db=$this->getAdapter();
		
		$counts=$db->fetchOne("SELECT COUNT(*) FROM $this->_name");

		return $counts;
		
	}

	public function getFenleiByPid($pid, $limit = 0) {

		$db=$this->getAdapter();
		$limitStr = '';
		if ($limit) {
			$limitStr = ' limit 0,' . $limit;
		}
		if ($this->_name == 'zcshop_productfls') {
			$thisArray=$db->fetchAll("SELECT * FROM $this->_name WHERE pid='$pid' and openflag='1' ORDER BY $this->order$limitStr");
		} else {
			$thisArray=$db->fetchAll("SELECT * FROM $this->_name WHERE pid='$pid' ORDER BY $this->order$limitStr");
		}

		return $thisArray;

	}
        
	public function getFenleiTree(){
		
		$fenleiArray=$this->getAllFenlei();
    	
    	$tree = new Tree('fenleiTree');
    	
    	$tree->addRoot(-1,array('flid'=>0,'pid'=>-1,'flname'=>'root'));
    	
    	foreach ($fenleiArray as $value){

    		$tree->addLeaf($value['flid'],$value['pid'],array('flid'=>$value['flid'],'pid'=>$value['pid'],'flname'=>$value['flname'],'en_flname'=>$value['en_flname']));

    	}

    	return $tree;
    	
		
	}
	

	/*
	public function setFenleiStringJS($type, $thisId = NULL) {

		$db=$this->getAdapter();

		if ($thisId == NULL) {
			
			$thisArray = $db->fetchAll ( "SELECT * FROM $this->_name WHERE pid='0' ORDER BY flid ASC" );
			
			$num = count ( $thisArray );

			for($i = 0; $i < $num; $i ++) {

				$this->fenleiString .= '<div class="fenleiHDiv">';

				$this->fenleiString .= '<a class="eachFenlei" onclick="changeFl(this,\'' . $thisArray [$i] ['flid'] . '\');">' . $thisArray [$i] ['flname'] . '</a>';

				$this->setFenleiStringJS ( $type, $thisArray [$i] ['flid'] );

				$this->fenleiString .= '</div>';

			}

		} 

		else {

			$thisArray = $db->fetchAll ( "SELECT * FROM $this->_name WHERE pid='$thisId' ORDER BY flid ASC" );

			$num = count ( $thisArray );

			if ($thisArray[0]['flid']!="") {

				for($i = 0; $i < $num; $i ++) {

					$this->fenleiString .= '<div class="fenleiHDivChild" style="padding-left:20px;display:none;">';

					$this->fenleiString .= '<a class="eachFenlei" onclick="changeFl(this,\'' . $thisArray [$i] ['flid'] . '\');">' . $thisArray [$i] ['flname'] . '</a>';

					$this->setFenleiStringJS ( $type, $thisArray [$i] ['flid'] );

					$this->fenleiString .= '</div>';

				}

			}

		}

	}
	
	
	public function setFenleiString($type, $thisId = NULL) {

		$db=$this->getAdapter();

		if ($thisId == NULL) {
			
			$thisArray = $db->fetchAll ( "SELECT * FROM $this->_name WHERE pid='0' ORDER BY flid ASC" );
			
			$num = count ( $thisArray );

			for($i = 0; $i < $num; $i ++) {

				$this->fenleiString.='<div class="fenleiHDiv">';
				
				$this->fenleiString.='<a class="eachFenlei" onclick="showflOrItems(this,\''.$type.'\',\''.$thisArray[$i]['flid'].'\');">'.$thisArray[$i]['flname'].'</a>';
				
				$this->setFenleiString($type,$thisArray[$i]['flid']);
				
				$this->fenleiString.='</div>';

			}

		} 

		else {

			$thisArray = $db->fetchAll ( "SELECT * FROM $this->_name WHERE pid='$thisId' ORDER BY flid ASC" );

			$num = count ( $thisArray );


			if ($thisArray[0]['flid']!="") {
				

				for($i = 0; $i < $num; $i ++) {

					$this->fenleiString.='<div class="fenleiHDivChild" style="padding-left:20px;display:none;">';
					
					$this->fenleiString.='<a class="eachFenlei" onclick="showflOrItems(this,\''.$type.'\',\''.$thisArray[$i]['flid'].'\');">'.$thisArray[$i]['flname'].'</a>';
					
					$this->setFenleiString($type,$thisArray[$i]['flid']);
					
					$this->fenleiString.='</div>';

				}

			}

		}

	}


	public function showAllfenlei($type,$flid){

		$db=$this->getAdapter();
		
		$pid=$db->fetchOne("SELECT pid FROM ".$this->_name." WHERE flid='$flid'");	

		$thisArray=$db->fetchAll("SELECT * FROM " . $this->_name . " WHERE pid='$pid'");

		$thisString.='<select onchange="changeFl(\''.$type.'\',this);"><option value="0">請選擇子分類</option>';

		for($i=0;$i<count($thisArray);$i++){

			if($thisArray[$i]['flid']==$flid){

				$thisString.='<option selected value="'.$thisArray[$i]['flid'].'">'.$thisArray[$i]['flname'].'</option>';

			}

			else{

				$thisString.='<option value="'.$thisArray[$i]['flid'].'">'.$thisArray[$i]['flname'].'</option>';

			}

		}

		$thisString.='</select>';

		$this->fenleiString=$thisString.$this->fenleiString;

		if($pid!=0){

			$this->showAllfenlei($type,$pid);

		}

	}


	


	


	public function showAllFenleiIndex($flid,$type){
		
		$db=$this->getAdapter();

		$thisArray=$db->fetchAll("SELECT * FROM " . $this->_name . " WHERE pid='$flid' ORDER BY flid ASC");

		for($i=0;$i<count($thisArray);$i++){

			$this->fenleiArray[$flid][$i]['flname']=$thisArray[$i]['flname'];

			$this->fenleiArray[$flid][$i]['flid']=$thisArray[$i]['flid'];

			$this->fenleiString.='<div>'.$thisArray[$i]['flname'].'</div>';

			$thisArray2=$db->fetchAll("SELECT * FROM " . $this->_name . " WHERE pid='".$thisArray[$i]['flid']."'");

			if(count($thisArray2)==0){

				continue;

			}

			for($j=0;$j<count($thisArray2);$j++){

				$this->fenleiArray[$thisArray[$i]['flid']][$j]['flname']=$thisArray2[$j]['flname'];

				$this->fenleiArray[$thisArray[$i]['flid']][$j]['flid']=$thisArray2[$j]['flid'];	

				$this->fenleiString.='<div>'.$thisArray2[$j]['flname'].'</div>';

				$this->showAllFenleiIndex($thisArray2[$j]['flid'],$type);

			}		

		}

	}
	*/

	


	public function getFenLeiNameByFlid($flid,$lang='cn') {
		
		$db=$this->getAdapter();

		if ($flid == '0') {
			
			return "wrong argument";

		}
		if($lang=='cn'){
			
			$flname=$db->fetchOne ( "SELECT flname FROM " . $this->_name . " WHERE flid='$flid'" );
			
		}else{
			
			$flname=$db->fetchOne ( "SELECT en_flname FROM " . $this->_name . " WHERE flid='$flid'" );
			
		}

		return $flname;

	}

	
	public function getFenLeiNameByFlidArray($flidArray) {
		
		$db=$this->getAdapter();
		
		$flString=join(',',$flidArray);
			
		$flnameArray=$db->fetchAll( "SELECT * FROM " . $this->_name . " WHERE flid IN ($flString)" );

		return $flnameArray;

	}
	
	public function getAllFenleiManagement($data,$flid,$class='fenleiEffection'){
		
		static $a=0;
		
		if($a==0){
			
			$this->fenleiString.='<div>';
			
		}else{
			
			$this->fenleiString.='<div class="smallChildDiv">';
			
		}
		
		$a=1;

		for($i=0;$i<count($data);$i++){
			
			if($data[$i]['pid']==$flid){
				
				$this->fenleiString.='<div class="fenleiParentDiv">';

				//$this->fenleiString.='<div class="bigChildDiv"><a title="'.$data[$i]['flname'].'" alt="'.$data[$i]['openflag'].'" name="'.$data[$i]['flid'].'" onclick="'.$class.'.showOrHide(this,'.$data[$i]['flid'].');">'.$data[$i]['en_flname'].'</a>&nbsp;&nbsp;&nbsp;<a style="display:none">'.$data[$i]['flbody'].'</a> ------ <a class="flLable" href="javascript:;" onclick="flManagement.alterfl(this);">修改</a>　<a class="flLable"  href="javascript:;" onclick="flManagement.newfl(this);">新增</a>　<a class="flLable" onclick="flManagement.delfl(this);">删除</a></div>'."\n";

                                if($data[$i]['pid']==0)
                                {
                                    $lagnappestr=' ----- <a class="flLable" href="administrator-lagnappe-index?itemid='.$data[$i]['flid'].'">赠品管理</a>------ <a class="flLable" href="administrator-fenlei-sizeurl?itemid='.$data[$i]['flid'].'">尺寸说明</a>';
                                }else
                                {
                                    $lagnappestr="";
                                }

                                $this->fenleiString.='<div class="bigChildDiv"><a title="'.$data[$i]['flname'].'" alt="'.$data[$i]['openflag'].'" name="'.$data[$i]['flid'].'" onclick="'.$class.'.showOrHide(this,'.$data[$i]['flid'].');">'.$data[$i]['en_flname'].'</a>&nbsp;&nbsp;&nbsp;<a style="display:none">&nbsp;&nbsp;</a> ------ <a class="flLable" href="administrator-fenlei-edit?itemid='.$data[$i]['flid'].'">修改</a>　<a class="flLable"  href="javascript:;" onclick="flManagement.newfl(this);">新增</a>　<a class="flLable" onclick="flManagement.delfl(this);">删除</a> '.$lagnappestr.'</div>'."\n";

				$thisFlid=$data[$i]['flid'];

				$data[$i]=null;
				
				if(count($data)>0){
					
					$this->getAllFenleiManagement($data,$thisFlid,$class);
						
				}
				$this->fenleiString.='</div>';
				
			}
			
		}
		
		$this->fenleiString.='</div>';
		
	}


        public function getAlllocationFenleiManagement($data,$flid,$class='fenleiEffection'){

		static $a=0;

		if($a==0){

			$this->fenleiString.='<div>';

		}else{

			$this->fenleiString.='<div class="smallChildDiv">';

		}

		$a=1;

		for($i=0;$i<count($data);$i++){

			if($data[$i]['pid']==$flid){

				$this->fenleiString.='<div class="fenleiParentDiv">';

				//$this->fenleiString.='<div class="bigChildDiv"><a title="'.$data[$i]['flname'].'" alt="'.$data[$i]['openflag'].'" name="'.$data[$i]['flid'].'" onclick="'.$class.'.showOrHide(this,'.$data[$i]['flid'].');">'.$data[$i]['en_flname'].'</a>&nbsp;&nbsp;&nbsp;<a style="display:none">'.$data[$i]['flbody'].'</a> ------ <a class="flLable" href="javascript:;" onclick="flManagement.alterfl(this);">修改</a>　<a class="flLable"  href="javascript:;" onclick="flManagement.newfl(this);">新增</a>　<a class="flLable" onclick="flManagement.delfl(this);">删除</a></div>'."\n";

                                if($data[$i]['pid']==0)
                                {
                                    $lagnappestr='';
                                }else
                                {
                                    $lagnappestr="";
                                }

                                $this->fenleiString.='<div class="bigChildDiv"><a title="'.$data[$i]['flname'].'" alt="'.$data[$i]['openflag'].'" name="'.$data[$i]['flid'].'" onclick="'.$class.'.showOrHide(this,'.$data[$i]['flid'].');">'.$data[$i]['en_flname'].'</a>&nbsp;&nbsp;&nbsp;<a style="display:none">&nbsp;&nbsp;</a> ------ <a class="flLable" href="administrator-location-edit?itemid='.$data[$i]['flid'].'">修改</a>　<a class="flLable"  href="javascript:;" onclick="flManagement.newfl(this);">新增</a>　<a class="flLable" onclick="flManagement.delfl(this);">删除</a> '.$lagnappestr.'</div>'."\n";

				$thisFlid=$data[$i]['flid'];

				$data[$i]=null;

				if(count($data)>0){

					$this->getAlllocationFenleiManagement($data,$thisFlid,$class);

				}
				$this->fenleiString.='</div>';
			}
		}

		$this->fenleiString.='</div>';

	}

	public function getAllFenleiByPid($from,$counts,$pid){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'*');
		if($this->_name==R_PRE . "product" . "fls")
		{
			$select->where("openflag=1");
		}
		
        $select->where("pid=$pid");
		$select->order("integral DESC");
		$select->limit($counts, $from);
		
		return $db->fetchAll($select);
		
	}
	
	public function getAllFlCountsByPid($pid){
		
		$db=$this->getAdapter();
		
		$counts=$db->fetchOne("SELECT COUNT(*) FROM $this->_name WHERE pid=$pid");

		return $counts;
		
	}
	
	public function refreshIntegral()
	{
		$db=$this->getAdapter();
		$db->query("CREATE TEMPORARY TABLE tmp_integralrefresh select `zcshop_members`.`clubid`,sum(`zcshop_members`.`integral`) as integralt from `zcshop_members` group by `zcshop_members`.`clubid`");
		$db->query("update `zcshop_productfls` s1 inner join `tmp_integralrefresh` s2 on s1.`flid` = s2.`clubid` set s1.`integral` = s2.`integralt` where s1.`flid` = s2.`clubid`");
	}
}





